package com.qdlc.p2p.manage.action;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.GoodsCategoryService;
import com.qdlc.p2p.biz.service.GoodsPicService;
import com.qdlc.p2p.biz.service.GoodsService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.enums.EnumUploadImgType;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.exception.ScoreException;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.checkrule.ScoreBaseInfoRuleCheck;
import com.qdlc.p2p.dal.common.RuleCheckHelper;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.Goods;
import com.qdlc.p2p.dal.dto.GoodsCategory;
import com.qdlc.p2p.dal.dto.GoodsPic;
import com.qdlc.p2p.dal.model.GoodsModel;

/**
 * 商品管理
 * @author xxx
 * @version 2.0
 * @date 2015年6月17日 下午1:30:29
 */
public class ManageGoodsAction extends BaseAction<GoodsModel> implements ModelDriven<GoodsModel> {
	
	@Resource
	private GoodsService goodsService;
	@Resource
	private GoodsCategoryService goodsCategoryService;
	@Resource
	private GoodsPicService goodsPicService;
	
	private Map<String, Object> data;
	/** 积分基本规则 **/
	private ScoreBaseInfoRuleCheck scoreBaseInfoRuleCheck = RuleCheckHelper.getValue(ScoreBaseInfoRuleCheck.class);
	
	@Action("/modules/user/score/goods/manage")
	public String goodsManager(){
		boolean needVerify = scoreBaseInfoRuleCheck.need_verify;
		request.setAttribute("need_verify", needVerify);
		List<GoodsCategory> goodsCategoryList = goodsCategoryService.list();
		request.setAttribute("goodsCategoryList", goodsCategoryList);
		return "manage";
	}
	
	@Action("/modules/user/score/goods/list")
	public void list() throws Exception{
		if(StringUtil.isBlank(paramString("status"))){
			model.setStatus(99);
		}
		if(StringUtil.isBlank(paramString("type"))){
			model.setType(99);	
		}
		PageDataList<GoodsModel> page = goodsService.findPageList(model);
		data = new HashMap<String, Object>();
		data.put("total", page.getPage().getTotal()); // 总行数
		data.put("rows", page.getList()); // 集合对象
		printJson(getStringOfJpaObj(data));
	}
	
	@Action("/modules/user/score/goods/checkGoodsName")
	public void checkGoodsName(){
		goodsService.checkGoodsName(model.getName());
	}
	
	/**
	 * 后台上传图片
	 * @throws Exception
	 * if has error
	 */
	@Action("/modules/user/score/goods/uploadPic")
	public void uploadPic() throws Exception {
		String picPath = imgUpload(EnumUploadImgType.GOODS.getValue());
		data = new HashMap<String, Object>();
		data.put("picPath", picPath);
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 后台删除图片
	 * @throws Exception
	 * if has error
	 */
	@Action("/modules/user/score/goods/delPic")
	public void delPic() throws Exception {
		String picPath = paramString("picPath");
		String realPath = ServletActionContext.getServletContext().getRealPath(picPath);
		new File(realPath).delete();
		printSuccess();
	}
	
	@Action("/modules/user/score/goods/append")
	public String append(){
		List<GoodsCategory> goodsCategoryList = goodsCategoryService.enableList();
		request.setAttribute("goodsCategoryList", goodsCategoryList);
		return "append";
	}
	
	@Action("/modules/user/score/goods/doAppend")
	public void doAppend() throws Exception{
		String picPath = imgUpload(EnumUploadImgType.GOODS.getValue());
		if (StringUtil.isBlank(picPath)) {
			throw new ScoreException("图片不能为空", BusinessException.TYPE_JSON);
		}
		model.checkModel();
		model.setPicPath(picPath);
		model.setGoodsCategory(new GoodsCategory(paramLong("categoryId")));
		model.setAddOperator(getOperator());
		goodsService.save(model.prototype());
		printSuccess();
	}
	
	@Action("/modules/user/score/goods/modify")
	public String modify(){
		List<GoodsCategory> goodsCategoryList =goodsCategoryService.enableList();
		Goods goods = goodsService.findById(model.getId());
		request.setAttribute("goods", goods);
		request.setAttribute("goodsCategoryList", goodsCategoryList);
		request.setAttribute("category", goods.getGoodsCategory());
		return "modify";
	}
	
	@Action("/modules/user/score/goods/doModify")
	public void doModify() throws Exception {
		model.setUpdateOperator(getOperator());
		Goods goods = model.prototypeForModify();
		String picPath = imgUpload(EnumUploadImgType.GOODS.getValue());
		if (StringUtil.isNotBlank(picPath)) {
			goods.setPicPath(picPath);
		}
		model.checkModel();
		goodsService.update(goods);
		printSuccess();
	}
	
	
	@Action("/modules/user/score/goods/verify")
	public String verify(){
		Goods goods = goodsService.findById(model.getId());
		List<GoodsPic> picList = goodsPicService.getGoodsPicByGoodsId(goods.getId());
		request.setAttribute("goods", goods);
		request.setAttribute("picList", picList);
		request.setAttribute("category", goods.getGoodsCategory());
		return "verify";
	}
	
	@Action("/modules/user/score/goods/doVerify")
	public void doVerify() throws Exception{
//		String description = paramString("description");
//		String attribute = paramString("attribute");
		model.setOperatorId(this.getOperatorId());
		goodsService.verify(model);
		printSuccess();
	}
	
	@Action("/modules/user/score/goods/shelves")
	public String shelves(){
		boolean needVerify = scoreBaseInfoRuleCheck.need_verify;
		request.setAttribute("needVerify", needVerify);
		Goods goods = goodsService.findById(model.getId());
		List<GoodsPic> picList = goodsPicService.getGoodsPicByGoodsId(goods.getId());
		request.setAttribute("goods", goods);
		request.setAttribute("picList", picList);
		request.setAttribute("category", goods.getGoodsCategory());
		return "shelves";
	}
	
	@Action("/modules/user/score/goods/doShelves")
	public void doShelves() throws Exception{
		model.setOperatorId(this.getOperatorId());
		model.setNeedVerify(scoreBaseInfoRuleCheck.need_verify);
		goodsService.shelves(model);
		printSuccess();
	}
}
